Jquery / Forms / Set-Get a input field & html element content
Set get a input field value in a form and html element content
-
1. Form fields value
using val()
The jQuery val() method is mainly used to get or set the current value of the HTML form elements such as input,select and textarea.
id or class reference are used for accessing input fields syntax:// to get value $(referenceElement).val(); //to set value $(referenceElement).val('hai'); in html
to get value in jquery
to set value in jquery//using id var n=$('#username').val(); //using class var n=$('.username').val(); //using id var n=$('#username').val('manoj'); //using class var n=$('.username').val('manoj'); -
2. HTML element content
1. Using text()
The jQuery text() method is used to get/set the text contents of the selected elements (Html content is not allowed)
Get Contents
Set ContentsHi manoj
var str = $("p").text();Hi manoj
$("p").text('Hi Vineetha');2. Using html()
The jQuery html() method is used to get or set the HTML contents of the elements.
Get Contents
Set ContentsHi manoj
var str = $("p").html();Hi manoj
$("p").html('Hi Vineetha');